﻿Imports System.Windows.Forms


Public Class PatternDialog
    Public NewProviderPattern As Boolean

    Private Sub CalcTotalBox()
        Dim r As Integer
        Dim p As Decimal

        p = 0
        For r = 0 To PatternGrid.RowCount - 1

            Try
                p = p + PatternGrid.Item(1, r).Value
            Catch ex As Exception
                TotalBox.Text = "Error"
                Exit Sub
            End Try

        Next

        TotalBox.Text = p.ToString

    End Sub

    Function ValidateGrid() As Boolean
        Dim r As Integer
        Dim p As Decimal

        'needs to add up to 100%
        p = 0
        For r = 0 To PatternGrid.RowCount - 1
            If PatternGrid.Item(1, r).Value.ToString <> "" Then
                Try
                    p = p + PatternGrid.Item(1, r).Value
                Catch ex As Exception
                    MessageBox.Show("The pattern needs to add up to 100%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Function
                End Try
            End If
        Next

        If p <> 100 Then
            MessageBox.Show("The pattern needs to add up to 100%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ValidateGrid = False
        Else
            ValidateGrid = True
        End If

    End Function

    Public Sub LoadRegionBox()
        Dim sql As String
        Dim TheView As DataView

        sql = "Select * from Regions"
        TheView = LoadDataView(sql)

        RegionBox.DataSource = TheView
        RegionBox.ValueMember = "Region_Code"
        RegionBox.DisplayMember = "Region_Name"
    End Sub

    Public Sub LoadLABox()
        Dim sql As String
        Dim TheView As DataView

        sql = "Select * from Local_Authorities" & _
              " where Region_Code = '" & RegionBox.SelectedValue.ToString & "'"
        TheView = LoadDataView(sql)

        LABox.DataSource = TheView
        LABox.ValueMember = "LA_Code"
        LABox.DisplayMember = "LA_Name"
    End Sub

    Public Sub LoadProviderBox()
        Dim sql As String
        Dim TheView As DataView

        sql = "Select * from all_provider_fields" & _
              " where LA_Code  = '" & LABox.SelectedValue.ToString & "'"
        TheView = LoadDataView(sql)

        ProvidersBox.DataSource = TheView
        ProvidersBox.ValueMember = "RID"
        ProvidersBox.DisplayMember = "Provider_Name"
    End Sub

    Private Sub ProviderPattern()
        Dim Period As DataView
        Dim Sql As String
        Dim StartDate, EndDate As Date
        Dim r As Integer

        If NewProviderPattern = True Then
            LoadPattern(BudgetsForm.BPatternGrid.Item(0, BudgetsForm.BPatternGrid.CurrentRow.Index).Value.ToString)

            'New pattern

            ' Sql = "Select * from Periods where Period_ID = " & BudgetsForm.PeriodBox.SelectedValue.ToString

            ' Period = LoadDataView(Sql)
            ' StartDate = Period(0).Item("Start_Date").ToString
            ' EndDate = Period(0).Item("End_Date").ToString
            ' PatternGrid.RowCount = 0
            ' PatternGrid.ColumnCount = 3
            ' PatternGrid.Columns(2).Visible = False
            ' PatternGrid.Columns(0).ReadOnly = True


            ' r = -1

            ' While StartDate <= EndDate
            'PatternGrid.Rows.Add()
            'r = r + 1
            'PatternGrid.Item(0, r).Value = MonthName(StartDate.Month) & " " & StartDate.Year
            'PatternGrid.Item(1, r).Value = "0.000000"
            'PatternGrid.Item(2, r).Value = StartDate

            'StartDate = StartDate.AddMonths(1)

            'End While

        Else
            Sql = "Select * from Provider_Patterns where " & _
                  " Period_ID = " & BudgetsForm.PeriodBox.SelectedValue.ToString & _
                  " and Allocation_ID = '" & BudgetsForm.AllocationLinesGrid.Item(BudgetsForm.AllocationLinesGrid.Columns("ID").Index, BudgetsForm.AllocationLinesGrid.CurrentRow.Index).Value.ToString & "'" & _
                  " and RID = '" & BudgetsForm.ProviderPatternGrid.Item(BudgetsForm.ProviderPatternGrid.Columns("RID").Index, BudgetsForm.ProviderPatternGrid.CurrentRow.Index).Value.ToString & "'"

            Period = LoadDataView(Sql)

            PatternGrid.RowCount = 0
            'PatternGrid.ColumnCount = 3
            'PatternGrid.Columns(2).Visible = False
            PatternGrid.Columns(0).ReadOnly = True

            For r = 0 To Period.Count - 1
                PatternGrid.Rows.Add()
                'StartDate = Period(r).Item("Month")
                PatternGrid.Item(0, r).Value = FormatDateTime(Period(r).Item("Month"), DateFormat.ShortDate)
                PatternGrid.Item(1, r).Value = FormatNumber(Period(r).Item("Percentage") * 100, 6)
                'PatternGrid.Item(2, r).Value = StartDate
            Next

        End If
    End Sub

    Public Sub LoadPattern(ByVal Pattern_ID As String)


        Dim Period As DataView
        Dim Sql As String
        Dim r As Integer

        If IDBox.Enabled = True Then
            'New pattern

            If PaymentTypeBox.SelectedValue.ToString = "System.Data.DataRowView" Then Exit Sub


            Sql = "Select * from payment_dates where payment_ID = " & PaymentTypeBox.SelectedValue.ToString & " order by payment_date"

            Period = LoadDataView(Sql)
            'StartDate = Period(0).Item("Start_Date").ToString
            'EndDate = Period(0).Item("End_Date").ToString
            PatternGrid.RowCount = 0
            '  PatternGrid.ColumnCount = 3
            '  PatternGrid.Columns(2).Visible = False
            PatternGrid.Columns(0).ReadOnly = True


            For r = 0 To Period.Count - 1
                PatternGrid.Rows.Add()
                PatternGrid.Item(0, r).Value = FormatDateTime(Period(r).Item("Payment_Date"), DateFormat.ShortDate)
                PatternGrid.Item(1, r).Value = "0.000000"
            Next

        Else
            Sql = "Select * from Patterns where Pattern_ID = '" & Pattern_ID & "' order by Month"

            Period = LoadDataView(Sql)

            PatternGrid.RowCount = 0
            PatternGrid.Columns(0).ReadOnly = True

            For r = 0 To Period.Count - 1
                PatternGrid.Rows.Add()
                PatternGrid.Item(0, r).Value = FormatDateTime(Period(r).Item("Month"), DateFormat.ShortDate)
                PatternGrid.Item(1, r).Value = FormatNumber(Period(r).Item("Percentage") * 100, 6)
            Next

        End If
    End Sub


    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        If Me.Text = "Provider Payment Pattern" Then


            If ProvidersBox.Text = "" Then
                MessageBox.Show("You must select a provider", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                ProvidersBox.Focus()
                Exit Sub
            End If

        Else
            If IDBox.Text = "" Then
                MessageBox.Show("The ID cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                IDBox.Focus()
                Exit Sub
            End If

            If DescriptionBox.Text = "" Then
                MessageBox.Show("The Description cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                DescriptionBox.Focus()
                Exit Sub
            End If
        End If

        If ValidateGrid() = False Then Exit Sub

        Me.DialogResult = System.Windows.Forms.DialogResult.OK

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub PatternDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Me.Text = "Provider Payment Pattern" Then

            ProviderPattern()
       
        End If

    End Sub



    Private Sub PatternGrid_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PatternGrid.CellValueChanged
        CalcTotalBox()
    End Sub

 

 
    Private Sub RegionBox_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegionBox.SelectedValueChanged
        LoadLABox()

    End Sub

    Private Sub LABox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LABox.SelectedIndexChanged
        LoadProviderBox()
    End Sub

    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        With Search_Form
            .WhoSentForMe = "Patterns"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                'find the provider in the list
                With .SearchResults_DataViewGrid
                    RegionBox.SelectedValue = .Item(.Columns("Region_Code").Index, .CurrentRow.Index).Value.ToString
                    LABox.SelectedValue = .Item(.Columns("LA_Code").Index, .CurrentRow.Index).Value.ToString
                    ProvidersBox.SelectedValue = .Item(.Columns("RID").Index, .CurrentRow.Index).Value.ToString

                End With
            End If
        End With
        
    End Sub

    Private Sub PaymentTypeBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PaymentTypeBox.SelectedIndexChanged
        If IDBox.Enabled = True Then
            LoadPattern("")
        Else
            LoadPattern(IDBox.Text)
        End If

    End Sub
End Class
